package com.central.scheduler.spi.task;

import com.central.common.enums.Direct;
import com.central.common.properties.Property;
import com.central.scheduler.spi.utils.JSONUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * @author Tindy
 * @date 2021/10/26
 * @describe job params related class
 */
public abstract class AbstractParameters implements IParameters {

    /**
     * local parameters
     */
    protected List<Property> localParams;
    /**
     * local parameters
     */
    public List<Property> globalParams;

    /**
     * var pool
     */
    public List<Property> varPool;

    /**
     * get local parameters list
     * @return Property list
     */
    public List<Property> getLocalParams() {
        return localParams;
    }

    public void setLocalParams(List<Property> localParams) {
        this.localParams = localParams;
    }
    /**
     * get global parameters list
     * @return Property list
     */
    public List<Property> getGlobalParams() {
        return globalParams;
    }

    public void setGlobalParams(List<Property> globalParams) {
        this.globalParams = globalParams;
    }
    /**
     * get local parameters map
     * @return parameters map
     */
    public Map<String, Property> getLocalParametersMap() {
        if (localParams != null) {
            Map<String, Property> localParametersMaps = new LinkedHashMap<>();

            for (Property property : localParams) {
                localParametersMaps.put(property.getProp(),property);
            }
            return localParametersMaps;
        }
        return null;
    }

    /**
     * get local parameters map
     * @return parameters map
     */
    public Map<String, Property> getGlobalParametersMap() {
        if (globalParams != null) {
            Map<String, Property> globalParametersMaps = new LinkedHashMap<>();

            for (Property property : globalParams) {
                globalParametersMaps.put(property.getProp(),property);
            }
            return globalParametersMaps;
        }
        return null;
    }

    /**
     * get varPool map
     *
     * @return parameters map
     */
    public Map<String, Property> getVarPoolMap() {
        if (varPool != null) {
            Map<String, Property> varPoolMap = new LinkedHashMap<>();
            for (Property property : varPool) {
                varPoolMap.put(property.getProp(), property);
            }
            return varPoolMap;
        }
        return null;
    }

    public List<Property> getVarPool() {
        return varPool;
    }

    public void setVarPool(List<Property> varPool) {
        this.varPool=varPool;
    }

    public void dealOutParam(String result) {
        if (CollectionUtils.isEmpty(localParams)) {
            return;
        }
        List<Property> outProperty = getOutProperty(localParams);
        if (CollectionUtils.isEmpty(outProperty)) {
            return;
        }
        if (StringUtils.isEmpty(result)) {
            varPool.addAll(outProperty);
            return;
        }
        Map<String, String> taskResult = getMapByString(result);
        if (taskResult == null || taskResult.size() == 0) {
            return;
        }
        for (Property info : outProperty) {
            info.setValue(taskResult.get(info.getProp()));
            varPool.add(info);
        }
    }

    public List<Property> getOutProperty(List<Property> params) {
        if (CollectionUtils.isEmpty(params)) {
            return new ArrayList<>();
        }
        List<Property> result = new ArrayList<>();
        for (Property info : params) {
            if (info.getDirect() == Direct.OUT) {
                result.add(info);
            }
        }
        return result;
    }

    public List<Map<String, String>> getListMapByString(String json) {
        List<Map<String, String>> allParams = new ArrayList<>();
        ArrayNode paramsByJson = JSONUtils.parseArray(json);
        Iterator<JsonNode> listIterator = paramsByJson.iterator();
        while (listIterator.hasNext()) {
            Map<String, String> param = JSONUtils.toMap(listIterator.next().toString(), String.class, String.class);
            allParams.add(param);
        }
        return allParams;
    }

    /**
     * shell's result format is key=value$VarPool$key=value$VarPool$
     * @param result
     * @return
     */
    public static Map<String, String> getMapByString(String result) {
        String[] formatResult = result.split("\\$VarPool\\$");
        Map<String, String> format = new HashMap<>();
        for (String info : formatResult) {
            if (StringUtils.isNotEmpty(info) && info.contains("=")) {
                String[] keyValue = info.split("=");
                format.put(keyValue[0], keyValue[1]);
            }
        }
        return format;
    }

}

